package com.thinkgem.jeesite.modules.tb_driver.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgem.jeesite.common.utils.MD5Util;
import com.thinkgem.jeesite.common.utils.ToolsUtil;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.web.ErrorCodeAndMsg;
import com.thinkgem.jeesite.modules.tb_driver.entity.TbDriver;
import com.thinkgem.jeesite.modules.tb_driver.service.TbDriverService;
import com.thinkgem.jeesite.modules.tb_waybill.entity.TbWaybill;
import com.thinkgem.jeesite.modules.tb_waybill.service.TbWaybillService;

@Controller
@RequestMapping("app/tbDriver/")
public class TbDriverAppController extends BaseController{
	
	@Autowired
	private TbDriverService tbDriverService;
	@Autowired
	private TbWaybillService tbWaybillService;
	
	/**
	 * 登录
	 * @param request
	 * @param userName
	 * @param pwd
	 * @return
	 */
	@RequestMapping("driverLoging")
	@ResponseBody
	public Map<String,Object> driverLoging(HttpServletRequest request,@RequestParam("userName")String userName,@RequestParam("pwd")String pwd){
		Map<String,Object> results = new HashMap<String , Object>();
		try{
			if(!ToolsUtil.isTrue(userName))return returnError(ErrorCodeAndMsg.USER_NOT_NAME_CODE, ErrorCodeAndMsg.USER_NOT_NAME_CODE_MSG);
			TbDriver driver = tbDriverService.fingByTell(userName);
			if(driver == null){
				return returnError(ErrorCodeAndMsg.USER_NAME_CODE, ErrorCodeAndMsg.USER_NAME_CODE_MSG);
			}else if(!ToolsUtil.isTrue(pwd)){
				return returnError(ErrorCodeAndMsg.USER_PWD_CODE, ErrorCodeAndMsg.USER_PWD_CODE_MSG);
			}else if("0".equals(driver.getDriverState())){
				return returnError(ErrorCodeAndMsg.USER_STATE_CODE, ErrorCodeAndMsg.USER_STATE_CODE_MSG);
			}
			pwd =MD5Util.MD5(pwd);
			if(!pwd.equals(driver.getDriverPwd())){
				return returnError(ErrorCodeAndMsg.USER_NAME_PWD_CODE, ErrorCodeAndMsg.USER_NAME_PWD_CODE_MSG);
			}
			results.put("driver_id",driver.getId() );
			results.put("driver_name",driver.getDriverName()  );
			results.put("driver_id_code",driver.getDriverIdCode()  );// 身份证号码
			results.put("driver_isQualificationCertificate",driver.getIsQualificationCertificate()  );//使用拥有从业资格证
			results.put("driver_isDriverLicense", driver.getIsDriverLicense() );//是否拥有驾驶证
			results.put("driver_isEscort", driver.getIsEscort() );//是否拥有押运证
			results.put("driver_isPosts",driver.getIsPosts()  );//是否拥有上岗证			
		}catch(Exception e){
			e.printStackTrace();
			return returnError(">>>登陆异常", e);
		}
		return returnSuccess(results);
	}
	
	/**
	 * 司机接受任务
	 * @param request
	 * @param driver_id
	 * @param waybail_id
	 * @return
	 */
	@RequestMapping("driverOK")
	@ResponseBody
	public Map<String,Object> driverOK(HttpServletRequest request,@RequestParam("driver_id")String driver_id,@RequestParam("waybail_id")String waybail_id){
		try{
			TbWaybill tbWaybill = tbWaybillService.get(waybail_id);
			if(!"9".equals(tbWaybill.getState())){
				tbWaybill.setState("9");
				tbWaybillService.changeStatu(tbWaybill);
			}
			TbDriver driver = tbDriverService.get(driver_id);
			driver.setDriverType("9");
			tbDriverService.save(driver);			
		}catch(Exception e){
			e.printStackTrace();
			return returnError(">>>司机接受任务异常", e);
		}
		return returnSuccess();
	}	
}
